package com.ohuang.wallpapermanager.screen

import androidx.compose.foundation.Image
import androidx.compose.foundation.background
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.lazy.grid.GridCells
import androidx.compose.foundation.lazy.grid.LazyVerticalGrid
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.snapshots.SnapshotStateList
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.navigation.NavHostController
import androidx.navigation.compose.rememberNavController
import com.ohuang.wallpapermanager.R
import com.ohuang.wallpapermanager.compose.clickableSafe
import com.ohuang.wallpapermanager.compose.rememberFilePainterSafe
import com.ohuang.wallpapermanager.vm.WallPaperListViewModel

data class WallPaperListScreenState(val list: SnapshotStateList<WallPaperListScreenStateItem>)

data class WallPaperListScreenStateItem(val id: Long, val imageFile: String)

sealed class WallPaperListScreenEvent {
    object AddWallPaper : WallPaperListScreenEvent()
    class ClickWallPaper(val id: Long) : WallPaperListScreenEvent()
}

@Composable
fun WallPaperListScreen(
    isLockWall: Boolean,
    goBack: () -> Unit,
    navController: NavHostController,
    viewModel: WallPaperListViewModel = androidx.lifecycle.viewmodel.compose.viewModel()
) {

    LaunchedEffect(key1 = isLockWall, block = {
        viewModel.init(isLock = isLockWall)
    })
    Column(modifier = Modifier.fillMaxWidth()) {
        TopBar(title = "${if (isLockWall) "锁屏" else "桌面"}壁纸", backClick = goBack)
        WallPaperListScreenImpl(viewModel.wallPaperListScreenState.value, onEvent = {
            when (it) {
                is WallPaperListScreenEvent.AddWallPaper -> {
                    navController.navigate(Destination.editWallPaperInfo(isLock = isLockWall))
                }

                is WallPaperListScreenEvent.ClickWallPaper -> {
                    navController.navigate(
                        Destination.editWallPaperInfo(
                            isLock = isLockWall,
                            id = it.id
                        )
                    )
                }
            }
        })
    }
}

@Preview(backgroundColor = 0xffffffff, showBackground = true)
@Composable
private fun WallPaperListScreenShow() {
    WallPaperListScreen(true, {}, navController = rememberNavController())
}

@Composable
private fun WallPaperListScreenImpl(
    state: WallPaperListScreenState,
    onEvent: (WallPaperListScreenEvent) -> Unit
) {
    LazyVerticalGrid(columns = GridCells.Fixed(3), content = {
        item {
            AddImageItem(onEvent = onEvent)
        }
        items(count = state.list.size) {
            ImageItem(state.list[it], onEvent = onEvent)
        }
    })
}

@Composable
private fun AddImageItem(onEvent: (WallPaperListScreenEvent) -> Unit) {
    Box(
        modifier = Modifier
            .height(200.dp)
            .padding(10.dp)
            .clip(shape = RoundedCornerShape(10.dp))
            .background(Color(0x80eeeeee))
            .clickableSafe {
                onEvent(WallPaperListScreenEvent.AddWallPaper)
            }
    ) {
        Image(
            modifier = Modifier
                .size(60.dp)
                .align(Alignment.Center),
            painter = painterResource(id = R.drawable.iv_add_img),
            contentDescription = ""
        )
    }
}

@Composable
private fun ImageItem(
    item: WallPaperListScreenStateItem,
    onEvent: (WallPaperListScreenEvent) -> Unit
) {

    Box(
        modifier = Modifier
            .height(200.dp)
            .padding(10.dp)
            .clip(shape = RoundedCornerShape(10.dp))
            .background(Color(0x80eeeeee))
            .clickableSafe {
                onEvent(WallPaperListScreenEvent.ClickWallPaper(item.id))
            }
    ) {
        Image(
            modifier = Modifier.fillMaxSize(),
            painter = rememberFilePainterSafe(item.imageFile, maxPixSize = 300*600),
            contentDescription = "",
            contentScale = ContentScale.Crop
        )
    }
}


